//
//  HEQRPayView.m
//  HealthFamily
//
//  Created by 吴欧 on 2021/3/15.
//  Copyright © 2021 honglutec. All rights reserved.
//

#import "HEQRPayView.h"
#import "HEQRCodeUtils.h"
#import "LEEAlert.h"

@interface HEQRContentView : UIView

- (instancetype)initWithFrame:(CGRect)frame withUrl:(NSString *)url;

@property (nonatomic, copy) NSString *url;

@property (nonatomic, copy ) void (^closeBlock)(void);

@end



@implementation HEQRPayView

+ (void)showWithURL:(NSString *)url cancelBlock:(void (^)(void))block {
    HEQRContentView *warp = [[HEQRContentView alloc] initWithFrame:CGRectMake(0, 0, 280, 300) withUrl: url];
    warp.closeBlock = ^{
        block();
    };
    
    [LEEAlert alert].config
    .LeeCustomView(warp)
    .LeeHeaderInsets(UIEdgeInsetsMake(0, 0, 0, 0))
    #ifdef __IPHONE_13_0
    .LeeUserInterfaceStyle(UIUserInterfaceStyleLight)
    #endif
    .LeeShow();
}

@end


 

@implementation HEQRContentView

- (instancetype)initWithFrame:(CGRect)frame withUrl:(NSString *)url {
    if (self=[super initWithFrame:frame]) {
        _url = url;
        [self initUI];
    }
    return self;
}

- (void)initUI {
    // 标题
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = @"二维码支付";
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.font = DYBlodFont(18);
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview: titleLabel];
    
    // 关闭按钮
    UIImageView *colseButton = [UIImageView new];
    colseButton.image = [UIImage imageNamed:@"guide_close"];
    colseButton.contentMode = UIViewContentModeScaleAspectFill;
    colseButton.userInteractionEnabled = YES;
    [self addSubview:colseButton];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeButtonAction:)];
    [colseButton addGestureRecognizer:tap];
  
    
    // 二维码
    UIImageView *qrImageView = [UIImageView new];
    qrImageView.image = [HEQRCodeUtils createQRPayWithUrl: _url];
    [self addSubview:qrImageView];
    
    
    // 标题
    UILabel *noticeLb = [[UILabel alloc] init];
    noticeLb.text = @"请使用微信或者支付宝扫码支付";
    noticeLb.textColor = [UIColor blackColor];
    noticeLb.font = [UIFont systemFontOfSize:16.0f];
    noticeLb.textAlignment = NSTextAlignmentCenter;
    [self addSubview: noticeLb];
    
    [colseButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(15);
        make.height.width.offset(25);
        make.right.equalTo(self).offset(-15);
    }];
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(15);
        make.left.equalTo(self).offset(15);
        make.right.equalTo(self).offset(-15);
    }];
    
    [qrImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLabel.mas_bottom).offset(15);
        make.centerX.equalTo(self);
        make.height.width.offset(180);
    }];
    
    [noticeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(qrImageView.mas_bottom).offset(15);
        make.left.equalTo(self).offset(15);
        make.right.equalTo(self).offset(-15);
        make.bottom.equalTo(self).offset(-15);
    }];
}

- (void)closeButtonAction:(UIButton *)sender {
    KWeakSelf
    [LEEAlert closeWithCompletionBlock:^{
        if (weakSelf.closeBlock) {
            weakSelf.closeBlock();
        }
    }];
}

@end
